package com.meilai.project.controller.mini;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.dto.system.UserSetRoleDTO;
import com.meilai.project.dto.system.WebUserDTO;
import com.meilai.project.entity.system.User;
import com.meilai.project.service.personnel.org.RoleService;
import com.meilai.project.service.system.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("api/mini/user")
@Api(tags = "小程序-用户")
public class MiniUserController {

    @Autowired
    private UserService service;

    @Autowired
    private RoleService roleService;

    @GetMapping("list")
    @ApiOperation(value = "列表")
    public CommonResult list(@RequestParam(name = "page", defaultValue = "1") Integer page,
                             @RequestParam(name = "page_size", defaultValue = "10") Integer page_size,
                             @RequestParam(name = "name", required = false) String name,
                             @RequestParam(name = "username", required = false) String username,
                             @RequestParam(name = "role_id", required = false) Long role_id) {
        return new CommonResult().success(service.selectPageList(page, page_size, name, username, role_id));
    }

    @GetMapping("role_list")
    @ApiOperation(value = "角色待选项")
    public CommonResult roleList() {
        return new CommonResult().success(roleService.selectOptionList());
    }

    @GetMapping("department_for_choose")
    @ApiOperation(value = "部门待选项")
    public CommonResult departmentForChoose() {
        return new CommonResult().success(service.getDepartmentChooseList());
    }

    @PutMapping("set_role")
    @ApiOperation(value = "设置角色")
    public CommonResult setRole(@RequestBody UserSetRoleDTO dto) {
        if (service.setRole(dto)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed("操作失败");
    }

    @PutMapping("status")
    @ApiOperation(value = "更改状态")
    public CommonResult changeStatus(@RequestParam(value = "ids[]") Long ids, Integer status) {
        if (service.changeStatus(ids, status)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed("操作失败");
    }

    @PutMapping("delete")
    @ApiOperation(value = "删除")
    public CommonResult deleted(@RequestParam(value = "ids[]") List<Long> ids) {
        if (service.del(ids)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed("操作失败");
    }

    @PostMapping("add")
    @ApiOperation(value = "新增")
    public CommonResult add(@RequestBody WebUserDTO dto) {
        try {
            if (service.addOne(dto)) {
                return new CommonResult().success("新增成功");
            }
            return new CommonResult().failed("新增失败");
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @PostMapping("edit")
    @ApiOperation(value = "修改")
    public CommonResult edit(@RequestBody WebUserDTO dto) {
        if (service.editOne(dto)) {
            return new CommonResult().success("修改成功");
        }
        return new CommonResult().failed("修改失败");
    }

    @PutMapping("reset_password")
    @ApiOperation(value = "重置密码")
    public CommonResult changeStatus(@RequestParam(value = "id") Long id) {
        if (service.resetPassword(id)) {
            return new CommonResult().success("重置成功");
        }
        return new CommonResult().failed("重置失败");
    }

    @GetMapping("getSubordinateUser")
    @ApiOperation(value = "获取下属的用户信息")
    public CommonResult getSubordinateUser() {
        return new CommonResult().success(service.getSubordinateUser());
    }

    @GetMapping("user_info")
    @ApiOperation(value = "用户详情")
    public CommonResult userInfo(HttpServletRequest request) {
        Long user_id = Long.parseLong(request.getAttribute("tokenUserId").toString());
        return new CommonResult().success(service.getUserVOById("", user_id));
    }

    @GetMapping("is_salesperson")
    @ApiOperation(value = "判断当前用户是否是销售人员")
    public CommonResult is_salesperson() {
        return new CommonResult().success(service.is_salesperson());
    }

    @GetMapping("allList")
    @ApiOperation(value = "获取公司所有用户")
    public CommonResult allList() {
        return new CommonResult().success(service.list(Wrappers.<User>lambdaQuery().isNull(User::getDeleted_at)));
    }

    @GetMapping("list_by_role_code")
    @ApiOperation(value = "根据角色code获取用户数据")
    public ApiResult<List<User>> list_by_role_code(@RequestParam(value = "code") String code) {
        return ApiResult.ok(service.list_by_role_code(code));
    }
}
